package object.obj2D.bullet;

import javax.microedition.khronos.opengles.GL10;

import object.obj2D.explode.Explode;
import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import system.Graphics;
import system.MyCamera;

import com.game.R;

public class MGunBullet extends IBullet{
	
	static private GL10 gl = Graphics.GetGraphics();
	private Explode explode = new Explode();
	
	static private float m_width = 0.15f;
	static private float m_height = 0.15f;
	static private TexSquare m_bulletSquare;
	static private Texture m_texture;
	static int m_texID;
	
	private static final float TRANS_X = 0.6f;
	private static final float TRANS_Y = -0.65f;
	private static final float TRANS_Z = -2;
	
	private boolean isDisplay = false;
	
	static public void Init(){
		m_width = 0.15f;
		m_height = 0.15f;
		m_bulletSquare = new TexSquare(m_width, m_height);
		m_texture = new Texture();
		m_texID = m_texture.initTexture(R.drawable.bullet_machine_gun);
	}
	
	public MGunBullet(float alpha, float beta, float distance, int damage)
	{
		m_alpha = alpha;
		m_beta = beta;
		m_transX = TRANS_X;
		m_transY = TRANS_Y;
		m_transZ = TRANS_Z;
		
		m_dTransX = TRANS_X/10;
		m_dTransY = TRANS_Y/10;
		m_dTransZ = 1;
		
		m_distance = distance;
		SetDistance(m_distance);
		Start();
	}
	
	public void SetAlpha(float alpha)
	{
		m_alpha = alpha;
	}
	
	public void SetBeta(float beta)
	{
		m_beta = beta;
	}
	
	public void SetDistance(float distance)
	{
		m_distance = distance;
		m_dTransX = TRANS_X / (distance + TRANS_Z);
		m_dTransY = TRANS_Y / (distance + TRANS_Z);
		m_dTransZ = 1;
	}
	
	public float GetAlpha()
	{
		return m_alpha;
	}
	
	public float GetBeta()
	{
		return m_beta;
	}
	
	public void Start()
	{
		isDisplay = true;
	}
	
	public void Stop()
	{
		m_transX = TRANS_X;
		m_transY = TRANS_Y;
		m_transZ = TRANS_Z;
		
		isDisplay = false;
	}
	
	public boolean isPlaying()
	{
		return isDisplay;
	}
	
	public void Render()
	{
		if(isDisplay)
		{
			gl.glMatrixMode(GL10.GL_MODELVIEW);			
			gl.glPushMatrix();
			gl.glRotatef(-180 + m_beta, 0, 1.0f, 0);
			gl.glRotatef(90 - m_alpha, 1.0f, 0, 0);
			gl.glTranslatef(0, 0, m_transZ);
			gl.glTranslatef(m_transX, 0, 0);
			gl.glTranslatef(0, m_transY, 0);			
			m_texture.loadTexture(m_texID);
			m_bulletSquare.Render();
			gl.glRotatef(-90, 0, 1.0f, 0);
			m_bulletSquare.Render();
			gl.glPopMatrix();
		}
		else {
			explode.Render();
		}
	}

	@Override
	public void Update() {
		// TODO Auto-generated method stub
		if(isDisplay)
		{
			m_transX -= m_dTransX;
			m_transY -= m_dTransY;
			m_transZ -= m_dTransZ;

			if(m_transZ <= -m_distance)
			{
				m_transX = TRANS_X;
				m_transY = TRANS_Y;
				m_transZ = TRANS_Z;
				
				isDisplay = false;
			}
		}
	}
}
